package com.qf.controller;

import com.qf.pojo.SysUsers;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 * @author lixu
 */
@Controller
public class LoginController {


    /**
     * 提交登录
     */
    @GetMapping("/doLogin1")
    public String doLogin1(String username, String password, Model model,Boolean rememberMe){
        //懒：没写 ： 用户名或密码应该判断是否为null  空串

        //1:Subject 当前用户来进行登录
        Subject subject = SecurityUtils.getSubject();
        //2:登录
        //加密
        Md5Hash md5Hash = new Md5Hash(password,"gp14",1024);
        UsernamePasswordToken usernamePasswordToken =
                new UsernamePasswordToken(username,md5Hash.toString());
        if(null != rememberMe){
            //记住我
            usernamePasswordToken.setRememberMe(rememberMe);
        }
        try {
            subject.login(usernamePasswordToken);
            System.out.println("==认证成功==");



            //角色
            //boolean gly = subject.hasRole("管理员");
            //权限
            //boolean permitted = subject.isPermitted("sys:user:*");


        /*    PrincipalCollection principals = subject.getPrincipals();

            SysUsers primaryPrincipal = (SysUsers) principals.getPrimaryPrincipal();*/

            //代码获取会话管理中用户对象  true
            System.out.println("是否认证:" + subject.isAuthenticated());
            //获取真实名称
   /*         Object principal = subject.getPrincipal();
            System.out.println("真实名称：" + principal);*/

            //获取用户名
            //String username2 = (String) subject.getPrincipal();
     /*       SysUsers sysUsers = (SysUsers) subject.getPrincipal();
            String username1 = sysUsers.getUsername();
            System.out.println("用户名:" + username1);
            System.out.println("真实名称:" + sysUsers.getRealname());
            System.out.println("Id:" + sysUsers.getId());*/

            return "redirect:/index.jsp";
        } catch (AuthenticationException e) {
            //e.printStackTrace();
            //System.out.println("认证失败");
            model.addAttribute("error",e.getMessage());
        }
        //登录失败了  还在登录页面
        return "/login.jsp";

    }
    /**
     * 退出
     */
    @GetMapping("/logout")
    @ResponseBody
    public Map logout(){
        //退出
        Subject subject = SecurityUtils.getSubject();
        subject.logout();//将Session中的用户信息清除  将Cookie中清除  会话管理（清理管理）
        Map result = new HashMap<>();
        result.put("code",0);
        return result;
    }
}
